package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"hello/models"
	"time"
)

type BorrowController struct {
	beego.Controller
}

func (c *BorrowController) GetBorrowList() {
	page, _ := c.GetInt("page")
	size, _ := c.GetInt("size")
	if page <= 0 {
		page = 1

	}
	if size <= 0 {
		size = 5

	}
	//计算偏移
	offset := (page - 1) * size
	count := 0
	var borrowList []models.Borrow
	db.Offset(offset).Limit(size).Order("update_time desc").Find(&borrowList)
	for i := range borrowList {
		book := models.Book{}
		db.Where("id = ?", borrowList[i].BookId).First(&book)
		borrowList[i].Book = book
	}
	db.Table("borrow").Count(&count)
	c.Data["json"] = models.PageSuccess(models.Page{
		Count: count,
		Page:  page,
		Size:  size,
		Data:  borrowList,
	})
	c.ServeJSON()
}

func (c *BorrowController) GetBorrow() {
	bookId, err := c.GetInt("id")

	if err != nil {
		fmt.Println(err)
		return
	}

	borrow := models.Borrow{}
	db.Where("id = ?", bookId).First(&borrow)
	if borrow != (models.Borrow{}) {
		c.Data["json"] = models.Success(borrow)
		c.ServeJSON()
	}
	c.Data["json"] = models.Error(nil)
	c.ServeJSON()
}

func (c *BorrowController) UpdateBorrow() {
	borrowId, _ := c.GetInt("borrowId")
	borrower := c.GetString("borrower")
	bookId, _ := c.GetInt("bookId")
	remark := c.GetString("remark")
	status, _ := c.GetInt("status")
	returnTime := c.GetString("returnTime")
	reTime, _ := time.ParseInLocation("2006-01-02", returnTime, time.Local)
	borrow := models.Borrow{}
	m := map[string]interface{}{
		"borrow_id":   borrowId,
		"borrower":    borrower,
		"book_id":     bookId,
		"remark":      remark,
		"status":      status,
		"update_time": time.Now(),
		"return_time": reTime,
	}

	//查询当前借阅的图书
	dbBook := models.Book{}
	db.Where("book_id = ?", bookId).First(&dbBook)
	book := models.Book{Id: bookId}
	if dbBook.Id != bookId {
		//如果不等于说明更换了借阅图书，需要把更换前的状态设置为正常，并且设置当前更改的为借阅中
		db.Model(&dbBook).Update("status", 1)
		db.Model(&book).Update("status", 0)
	}

	//执行更新
	db.Model(&borrow).Updates(m)

	//已归还 设置图书状态为正常
	if status == 1 {
		db.Model(&book).Update("status", 1)
	}

	c.Data["json"] = models.Success(nil)
	c.ServeJSON()
}

func (c *BorrowController) SaveBorrow() {
	borrower := c.GetString("borrower")
	bookId, _ := c.GetInt("bookId")
	remark := c.GetString("remark")
	status, _ := c.GetInt("status")
	returnTime := c.GetString("returnTime")
	reTime, _ := time.ParseInLocation("2006-01-02", returnTime, time.Local)
	borrow := models.Borrow{
		Borrower:   borrower,
		BookId:     bookId,
		Remark:     remark,
		UpdateTime: time.Now(),
		CreateTime: time.Now(),
		Status:     status,
		ReturnTime: reTime,
	}

	db.Create(&borrow)
	//设置book为借阅中
	book := models.Book{Id: bookId}
	db.Model(&book).Update("status", 0)
	c.Data["json"] = models.Success(nil)
	c.ServeJSON()
}

func (c *BorrowController) DeleteBorrow() {
	borrowId, _ := c.GetInt("id")
	borrow := models.Borrow{BorrowId: borrowId}
	db.Delete(&borrow)
	c.Data["json"] = models.Success(nil)
	c.ServeJSON()
}

func (c *BorrowController) OneKeyReturn() {
	borrowId, _ := c.GetInt("id")
	borrow := models.Borrow{BorrowId: borrowId}

	m := map[string]interface{}{
		"status":      1,
		"update_time": time.Now(),
		"return_time": time.Now(),
	}
	db.Model(&borrow).Updates(m)
	//查询出相关的图书，更改状态为正常
	db.First(&borrow)
	bookId := borrow.BookId
	dbBook := models.Book{}
	db.Where("book_id = ?", bookId).First(&dbBook)
	db.Model(&dbBook).Update("status", 1)
}

func (c *BorrowController) GetUsableBookList(){
	bookId, err := c.GetInt("id")

	var bookList []models.Book
	db.Where("status = ?", 1).Find(&bookList)

	if bookId != 0 && err == nil {
		book := models.Book{}
		db.Where("id = ?", bookId).First(&book)
		fmt.Println(book)
		bookList = append(bookList, book)
	}

	c.Data["json"] = models.Success(bookList)
	c.ServeJSON()
}
